package CCRMClient.GUI;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JButton;

import CCRMClient.ClientStart;
import Common.SERVER_COMMANDS;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.border.LineBorder;

import java.awt.Color;
import java.io.IOException;

import javax.swing.SwingConstants;

public class EmployeePortal extends JPanel {

	private JLabel lblCcrmEmployee;
	private JButton btnMarketing;
	private JButton btnSales;
	private JButton btnReports;
	private JButton btnCustomerManagement;
	private JButton btnQuit;
	private JButton btnSearchAndViewProduct;
	/**
	 * Create the panel.
	 */
	public EmployeePortal() {

		initialize();
	}
	private void initialize() {
		setBackground(Color.WHITE);
		setBorder(new LineBorder(new Color(0, 0, 0)));
		setLayout(null);
		setSize(474, 606);
		
		lblCcrmEmployee = new JLabel("CCRM - Employee Portal");
		lblCcrmEmployee.setForeground(Color.RED);
		lblCcrmEmployee.setHorizontalAlignment(SwingConstants.CENTER);
		lblCcrmEmployee.setBounds(0, 41, 460, 24);
		lblCcrmEmployee.setFont(new Font("David", Font.BOLD, 26));
		add(lblCcrmEmployee);
		
		JButton btnMarketing = new JButton("Marketing");
		btnMarketing.setBounds(83, 92, 291, 50);
		btnMarketing.setFont(new Font("David", Font.PLAIN, 16));
		btnMarketing.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ClientStart.panelSwicher(new MarketingScreen());
			}
		});
		add(btnMarketing);
		
		btnSales = new JButton("Sales");
		btnSales.setBounds(83, 169, 291, 50);
		btnSales.setFont(new Font("David", Font.PLAIN, 16));
		btnSales.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.VIEW_SALES_FOR_SALESMAN, ClientStart.user.getID());
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();
				}
			}
		});
		add(btnSales);
		
		btnReports = new JButton("Reports");
		btnReports.setBounds(83, 242, 291, 50);
		btnReports.setFont(new Font("David", Font.PLAIN, 16));
		btnReports.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ClientStart.panelSwicher(new ReportsScreen());
			}
		});
		add(btnReports);
		
		btnQuit = new JButton("Quit");
		btnQuit.addActionListener(new BtnQuitActionListener());
		btnQuit.setBounds(340, 477, 89, 23);
		add(btnQuit);
		add(getBtnSearchAndViewProduct());
		add(getBtnCustomerManagement());
	}
	private class BtnQuitActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.LOG_OUT, null);
			} catch (IOException e1) {
				JOptionPane.showMessageDialog(null, "Client is down! Exiting application", "CCRMClient Error", JOptionPane.ERROR_MESSAGE);
				ClientStart.quitApp();
			}
		}
	}
	/**
	 * This inner class implements event listener for pressing Search And View Product button
	 * @author Ori Ziv
	 *
	 */
	private class BtnSearchAndViewProductActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			ClientStart.panelSwicher(new ProductSearchScreen());
		}
	}
	private JButton getBtnSearchAndViewProduct() {
		if (btnSearchAndViewProduct == null) {
			btnSearchAndViewProduct = new JButton("Search and View Products");
			btnSearchAndViewProduct.addActionListener(new BtnSearchAndViewProductActionListener());
			btnSearchAndViewProduct.setFont(new Font("David", Font.PLAIN, 16));
			btnSearchAndViewProduct.setBounds(83, 316, 291, 50);
		}
		return btnSearchAndViewProduct;
	}
	private JButton getBtnCustomerManagement(){
		if(btnCustomerManagement==null){
			btnCustomerManagement= new JButton("Customer Management");
			btnCustomerManagement.addActionListener(new BtnCustomerMenagementActionListener());
			btnCustomerManagement.setFont(new Font("David", Font.PLAIN, 16));
			btnCustomerManagement.setBounds(83, 394, 291, 50);
		}
		return btnCustomerManagement;
	}
	
	private class BtnCustomerMenagementActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			ClientStart.panelSwicher(new CustomerManage());
		}
}
}